package com.vcaresolution.logistic.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vcaresolution.logistic.to.DataHolderTO;
import com.vcaresolution.logistic.to.ShippingbillDetailsTO;
import com.vcaresolution.logistic.util.ApplicationConstants;



/**
 * <code>MasterController</code> is used to track all common operations
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class MasterController extends BaseController{

	/**
	 * @objective -: to get branchList associated with particular company.<br>
	 * @date -: 09-13-2013<br>
	 * @version -: 1.0<br>
	 * @since -: ShipSoft 1.0<br>
	 * @author -: Vishal<br>
	 **/
	@RequestMapping(value = "/GetBranchListForCompany", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	List<DataHolderTO> populateBranchForCompany(@RequestParam(required = true, value = "sourceId") final String companyId) {
		List<DataHolderTO> branchList = getServiceRegistry().getCompanyService().getAssociatedBranchList(companyId,DataHolderTO.class);
		return branchList;
	}
	
	/**
	 * @objective -: to get departmentList associated with particular branches.<br>
	 * @date -: 09-16-2013<br>
	 * @version -: 1.0<br>
	 * @since -: ShipSoft 1.0<br>
	 * @author -: Vishal<br>
	 **/
	@RequestMapping(value = "/GetDepartmentListForBranchList", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	Map<String, ? extends Object> populateDepartmentForBranch(@RequestParam(required = true, value = "sourceId") final Set<Integer> branchIdSet) 
	{
		Map<String, ? extends Object> branchDepartmentMap = getServiceRegistry().getBranchService().getAssociatedDepartmentList(branchIdSet);
		return branchDepartmentMap;
	}
	
	@RequestMapping(value = "/GetCountryListForSector", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	List<DataHolderTO> populateCountryForSector(@RequestParam(required = true, value = "sourceId") final String sectorId) {
		return getServiceRegistry().getSectorService().getAssociatedCountryList(Integer.parseInt(sectorId));
	}
	
	@RequestMapping(value = "/GetStateListForCountry", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	List<DataHolderTO> populateStateForCountry(@RequestParam(required = true, value = "sourceId") final String countryId) {
		return getServiceRegistry().getSectorService().getAssociatedStateList(Integer.parseInt(countryId));
	}
	
	@RequestMapping(value = "/GetCityListForState", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	List<DataHolderTO> populateCityForState(@RequestParam(required = true, value = "sourceId") final String stateId) {
		return getServiceRegistry().getSectorService().getAssociatedCityList(Integer.parseInt(stateId)); 
	}
	
	@RequestMapping(value = ApplicationConstants.JOB_AUTOCOMPLETE_URL, method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody
	List<DataHolderTO> populateJobs(@RequestParam(required = true, value = "jobno") final String jobno) {
		return getServiceRegistry().getJobRegisterService().populateJobs(jobno); 
	}
	
	@RequestMapping(value = "/AddCodeTable", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	DataHolderTO addCodeTable(@RequestParam(required = true, value = "formvalue") final String formValue, @RequestParam(required = true, value = "formdescription") final String formDescription) {
		System.out.println("Received :: "+formValue+" Description "+formDescription);
		
		DataHolderTO dataHolderTO = getServiceRegistry().getCodetableService().add(formValue, formDescription);;
		return  dataHolderTO;
	}
	
	/**
	 * This is used for Ajax call to retrieve Data.
	 * This method is called when the POST request has been made to the ShippingbillContainerList Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_SHIPPINGBILL_CONTAINER_MAPPING,method = RequestMethod.GET,produces = "application/json")
	public @ResponseBody
	List<DataHolderTO> containerListDetails() {
		return getServiceRegistry().getContainerService().populateContaineres();
	}
	
	/**
	 * This is used for Ajax call to retrieve Data.
	 * This method is called when the POST request has been made to the ShippingbilLAutoComplete Request
	 */
	@RequestMapping(value = ApplicationConstants.SHIPPINGBILL_AUTOCOMPLETE_URL, method = RequestMethod.GET,produces = "application/json")
	public @ResponseBody
	List<ShippingbillDetailsTO> populateShippingBill(@RequestParam(required = true, value = "sbillno") final String sbillno
			,final Model model, final HttpServletRequest request) {
		
		return getServiceRegistry().getShippingbillService().populateShippingBill(sbillno);
		
	}
}
